<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Example</title>
    <script src="base64.js"></script>
</head>
<body>
<h1>Base64.js</h1>
<p>加密data:</p>
<p><textarea id="input" cols="80" rows="10"></textarea></p>
<p>密钥secret:</p>
<p><input  id="secret" type=''/></p>
<br>
<!--<button onclick="normal_encode()">普通Base64编码</button>-->
<button onclick="normal_decode()">解码data</button>
<!--<button onclick="url_safe_encode()">URL_safe Base64编码</button>-->
<!--<button onclick="url_safe_decode()">URL_safe Base64解码</button>-->
<br>
<textarea id="output" cols="80" rows="10" readonly></textarea>

<script type="text/javascript">
    function normal_encode() {
        var raw = document.getElementById("input").value;
        var encoded = BASE64.encode(raw);
        document.getElementById("output").value = encoded;
    }

    function normal_decode() {
        var raw = document.getElementById("input").value;
		var secret = document.getElementById("secret").value;
        var decoded = BASE64.decode(raw);
		var finalVal = rc4(decoded,secret);
        document.getElementById("output").value = finalVal;
    }

    function url_safe_encode() {
        var raw = document.getElementById("input").value;
        var encoded = BASE64.urlsafe_encode(raw);
        document.getElementById("output").value = encoded;
    }

    function url_safe_decode() {
        var raw = document.getElementById("input").value;
        var decoded = BASE64.urlsafe_decode(raw);
        document.getElementById("output").value = decoded;
    }
	function rc4(data,key)
	{
		var pwd = key;
		var cipher      = '';
		var key       = [];
		var box       = [];
		var pwd_length  = pwd.length;
		var data_length = data.length;
		for (i = 0; i < 256; i++) {
			key[i] = pwd[i % pwd_length].charCodeAt();
			box[i] = i;
		}
		for (j = i = 0; i < 255; i++) {
			j = (j + box[i] + key[i]) % 256;
			tmp = box[i];
			box[i] = box[j];
			box[j] = tmp;
		}
		for (a = j = i = 0; i < data_length; i++) {
			a       = (a + 1) % 256;
			j       = (j + box[a]) % 256;
			tmp     = box[a];
			box[a] = box[j];
			box[j] = tmp;
			k       = box[((box[a] + box[j]) % 256)];
			cipher += String.fromCharCode(data[i].charCodeAt() ^ k);
		}
		return cipher;
	}
	
</script>
</body>
</html>